﻿using Model;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;

namespace Business
{
    public class LockingBiz
    {
        public int SaveItem(Locking item)
        {
            var dbContext = new Context();

            dbContext.Lockings.Add(item);
            dbContext.SaveChanges();

            return item.Id;
        }

        public void UpdateItem(Locking item)
        {
            var dbContext = new Context();
            dbContext.Entry(item).State = EntityState.Modified;
            dbContext.SaveChanges();
        }

        public Locking LoadItem(int id)
        {
            var dbContext = new Context();
            return (from item in dbContext.Lockings
                    where item.Id == id
                    select item).First();
        }
        
        public Locking LoadItem(string tableName, int currentUser, int itemId)
        {
            var dbContext = new Context();
            var result = (from item in dbContext.Lockings
                    where item.TableName == tableName && item.ItemId == itemId && item.CurrentUserId != currentUser && item.Status == Constants.LockedStatus
                    select item).ToList();

            if (result != null && result.Count > 0)
                return result[0];

            return null;
        }

        public void RemoveItem(List<Locking> items)
        {
            var dbContext = new Context();

            string ids = string.Empty;

            foreach (var item in items)
            {
                ids += item.Id + ",";
            }

            if (ids.EndsWith(","))
            {
                ids = ids.Substring(0, ids.Length - 1);

                string deleteCommand = string.Format(Constants.DeleteCommand, "Locking", ids);
                dbContext.Database.ExecuteSqlCommand(deleteCommand);
            }   
        }

        public List<LockingTemplate> LoadItems()
        {
            var context = new Context();
            return context.Database.SqlQuery<LockingTemplate>("GetLockingTemplate").ToList();
        }

        public bool IsLocked(string tableName, int itemId, int currentUser)
        {
            var item = LoadItem(tableName, currentUser, itemId);

            return item != null;
        }

        public void LockItem(string tableName, int itemId, int currentUser)
        {
            var item = new Locking();
            item.TableName = tableName;
            item.ItemId = itemId;
            item.CurrentUserId = currentUser;
            item.Status = Constants.LockedStatus;
            item.StartTime = DateTime.Now;

            SaveItem(item);
        }

        public void UnlockItem(string tableName, int itemId, int currentUser)
        {
            var dbContext = new Context();
            var result = (from item in dbContext.Lockings
                          where item.TableName == tableName && item.ItemId == itemId && item.CurrentUserId == currentUser && item.Status == Constants.LockedStatus
                          select item).ToList();

            if (result != null && result.Count > 0)
            {
                RemoveItem(result);
            }
        }
    }
}
